using System;

namespace HooIzDat.Web.Models
{
    public partial class CharacterTrait
    {
        public virtual int CharacterID
        {
            get { return _characterID; }
            set
            {
                try
                {
                    _settingFK = true;
                    if (_characterID != value)
                    {
                        if (Character != null && Character.CharacterID != value)
                        {
                            Character = null;
                        }
                        _characterID = value;
                    }
                }
                finally
                {
                    _settingFK = false;
                }
            }
        }
        private int _characterID;
    
        public virtual int TraitID
        {
            get { return _traitID; }
            set
            {
                try
                {
                    _settingFK = true;
                    if (_traitID != value)
                    {
                        if (Trait != null && Trait.TraitID != value)
                        {
                            Trait = null;
                        }
                        _traitID = value;
                    }
                }
                finally
                {
                    _settingFK = false;
                }
            }
        }
        private int _traitID;
    
        public virtual Nullable<int> TraitVariationID
        {
            get { return _traitVariationID; }
            set
            {
                try
                {
                    _settingFK = true;
                    if (_traitVariationID != value)
                    {
                        if (TraitVariation != null && TraitVariation.TraitVariationID != value)
                        {
                            TraitVariation = null;
                        }
                        _traitVariationID = value;
                    }
                }
                finally
                {
                    _settingFK = false;
                }
            }
        }
        private Nullable<int> _traitVariationID;

        public virtual Character Character
        {
            get { return _character; }
            set
            {
                if (!ReferenceEquals(_character, value))
                {
                    var previousValue = _character;
                    _character = value;
                    FixupCharacter(previousValue);
                }
            }
        }
        private Character _character;
    
        public virtual Trait Trait
        {
            get { return _trait; }
            set
            {
                if (!ReferenceEquals(_trait, value))
                {
                    var previousValue = _trait;
                    _trait = value;
                    FixupTrait(previousValue);
                }
            }
        }
        private Trait _trait;
    
        public virtual TraitVariation TraitVariation
        {
            get { return _traitVariation; }
            set
            {
                if (!ReferenceEquals(_traitVariation, value))
                {
                    var previousValue = _traitVariation;
                    _traitVariation = value;
                    FixupTraitVariation(previousValue);
                }
            }
        }
        private TraitVariation _traitVariation;

        private bool _settingFK = false;
    
        private void FixupCharacter(Character previousValue)
        {
            if (previousValue != null && previousValue.CharacterTraits.Contains(this))
            {
                previousValue.CharacterTraits.Remove(this);
            }
    
            if (Character != null)
            {
                if (!Character.CharacterTraits.Contains(this))
                {
                    Character.CharacterTraits.Add(this);
                }
                if (CharacterID != Character.CharacterID)
                {
                    CharacterID = Character.CharacterID;
                }
            }
        }
    
        private void FixupTrait(Trait previousValue)
        {
            if (previousValue != null && previousValue.CharacterTraits.Contains(this))
            {
                previousValue.CharacterTraits.Remove(this);
            }
    
            if (Trait != null)
            {
                if (!Trait.CharacterTraits.Contains(this))
                {
                    Trait.CharacterTraits.Add(this);
                }
                if (TraitID != Trait.TraitID)
                {
                    TraitID = Trait.TraitID;
                }
            }
        }
    
        private void FixupTraitVariation(TraitVariation previousValue)
        {
            if (previousValue != null && previousValue.CharacterTraits.Contains(this))
            {
                previousValue.CharacterTraits.Remove(this);
            }
    
            if (TraitVariation != null)
            {
                if (!TraitVariation.CharacterTraits.Contains(this))
                {
                    TraitVariation.CharacterTraits.Add(this);
                }
                if (TraitVariationID != TraitVariation.TraitVariationID)
                {
                    TraitVariationID = TraitVariation.TraitVariationID;
                }
            }
            else if (!_settingFK)
            {
                TraitVariationID = null;
            }
        }
    }
}
